
#include "pjsipwrapper.h"
#include <pj/log.h>
#include <string>
#include <fstream>
#include <iostream>

#define USING_TCP
using namespace std;

class MyAccount : public PjApiUaAccount
{
public:
    MyAccount(std::string username,
                    std::string password,
                    std::string domain,
                    std::string registar="",
                    std::string proxy="")
    :PjApiUaAccount(username, password, domain, registar, proxy)
    {}

};

class MyAccountCallback : public PjApiUaAccountCallback
{
public:
    MyAccountCallback(PjApiUaAccountHandler *handler)
    :PjApiUaAccountCallback(handler)
    {
    }

    void onIncomingCall(int account_id, int call_id, std::string message)
    {
         PJ_LOG(3,("On incomming call", "Test completed for call back"));
         getAccountHandler()->acceptCall(call_id);

    }

    void onIMMessage(int call_id,
                            const std::string from,
                            const std::string to,
                            const std::string contact,
                            const std::string mime_type,
                            const std::string body)
    {
        printf("*****\n from : %s, to: %s body: %s\n\n", from.c_str(), to.c_str(), body.c_str());
    }

    void onInfoRequest(int session_id, std::string request_name, std::string content_type, void *data)
    {
        char * p = (char*)data;
        printf("request name: %s, data is %s\n", request_name.c_str(), p);
    }
};

class MyAccountHandler : public PjApiUaAccountHandler
{

};

int main()
{
    #define USING_TCP
   PJ_LOG(3,("test", "Test start for the wrapper"));

    PjApiUa::getInstance()->init();


    MyAccount *account = new MyAccount("siptest001","123456", "211.100.17.183", "", "");
    PJ_LOG(3,("test", "1"));
    MyAccountHandler *handler = new MyAccountHandler();
    PJ_LOG(3,("test", "2"));
    MyAccountCallback *callback = new MyAccountCallback(handler);
    PJ_LOG(3,("test", "3"));

    PjApiUa::getInstance()->registerAccountHandler(handler);


    handler->setTransportConfig();
    handler->setRtpPort(4000);

    PjApiUa::getInstance()->start();
    handler->setAccount(account);
    PJ_LOG(3,("test", "4"));
    handler->setAccountCallback(callback);

    std::string to = "sip:siptest002@211.100.17.183";

    unsigned long long i = 0, j =0;


    //PjApiUa::getInstance()->onIncomingCall();



    //PjApiUa::getInstance()->createAccount("test", "sfs", "fdfd", "fdf");
#if 1
    ifstream p("Makefile.Debug");
    //p.open("./test_res/Opnss.org Blogs.htm");
   // p.open("main.cpp");
  // get length of file:
  p.seekg (0, ios::end);
  int length = p.tellg();
  p.seekg (0, ios::beg);

  // allocate memory:
  char *pp = new char [length];
  // read data as a block:
  p.read (pp,length);
  p.close();

  //  p.read(pp, p.gcount());

    //p.read(pp, p.gcount());
    printf("=========================================\n");
    //printf("msg: \n %s \n", pp);

    //printf("username: %s", PjApiUa::getInstance()->getUaAccount()->getUsername().c_str());

    while (i<100000000)
    {
        i++;
    }
   // int id = handler->makeCall(to);
    i = 0;
    while (i<1000000000)
    {
         i++;
    }
    i = 0;
    while (i<1000000000)
    {
         i++;
    }


#if 1
    std::string from = "sip:siptest001@211.100.17.183";
    handler->sendPackageStateless(to,
                                  from,
                                  to,
                                  from,
                                "Request::Blog1234567",
                                 "text/plain",
                                 "88888888777777776&&&&&&***");
#endif
    i=j=0;
#endif

#if 1

    while(1)
    {
    }
#endif

    PjApiUa::getInstance()->terminate();

    return 0;
}
